package me.luger.project.base.config.common;

import com.alibaba.fastjson.support.spring.FastJsonpHttpMessageConverter4;
import com.alibaba.fastjson.support.spring.FastJsonpResponseBodyAdvice;
import me.luger.core.auth.cookie.interceptor.CookieAuthInterceptor;
import me.luger.core.mvc.interceptor.access.AccessInterceptor;
import me.luger.core.mvc.interceptor.appInfo.AppInfoInterceptor;
import me.luger.core.mvc.interceptor.oplog.OpLogInterceptor;
import me.luger.core.mvc.version.CustomRequestMappingHandlerMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.List;

/**
 * Created by DELL on 2017/1/7.
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

        super.addResourceHandlers(registry);
    }

    @Bean
    public FastJsonpResponseBodyAdvice fastJsonpResponseBodyAdvice() {
        return new FastJsonpResponseBodyAdvice("callback", "jsonp");
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(0, new FastJsonpHttpMessageConverter4());
        super.extendMessageConverters(converters);
    }

    @Override
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping handlerMapping = new CustomRequestMappingHandlerMapping();
        handlerMapping.setOrder(0);
        handlerMapping.setInterceptors(getInterceptors());
        return handlerMapping;
    }

    @Autowired
    private RequestMappingHandlerAdapter adapter;

    @Bean
    public AccessInterceptor accessInterceptor(){
        return new AccessInterceptor();
    }

    @Bean
    public CookieAuthInterceptor cookieAuthoInterceptor(){
        return new CookieAuthInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
//        registry.addInterceptor(new AccessLogFilter()).addPathPatterns("/**");
//        registry.addInterceptor(new PageCacheInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(accessInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(cookieAuthoInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(new AppInfoInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(new OpLogInterceptor(adapter)).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
}
